Skip to content

Method: createCollectionNode(String, Collection)

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.serialization;
16:
17: import cz.cvut.kbss.jsonld.common.CollectionType;
18: import cz.cvut.kbss.jsonld.serialization.model.*;
19:
20: import java.util.Collection;
21: import java.util.Date;
22: import java.util.List;
23: import java.util.Set;
24:
25: /**
26: * Factory for constructing {@link JsonNode} instances.
27: */
28: public class JsonNodeFactory {
29:
30: private JsonNodeFactory() {
31: throw new AssertionError();
32: }
33:
34: private enum LiteralType {
35: BOOLEAN, NUMBER, STRING, TEMPORAL
36: }
37:
38: public static LiteralNode createLiteralNode(Object value) {
39: return createLiteralNode(null, value);
40: }
41:
42: public static LiteralNode createLiteralNode(String name, Object value) {
43: final LiteralType type = determineLiteralType(value);
44: LiteralNode node = null;
45: switch (type) {
46: case BOOLEAN:
47: node = name != null ? new BooleanLiteralNode(name, (Boolean) value) : new BooleanLiteralNode(
48: (Boolean) value);
49: break;
50: case NUMBER:
51: node = name != null ? new NumericLiteralNode<>(name, (Number) value) :
52: new NumericLiteralNode<>((Number) value);
53: break;
54: case STRING:
55: node = name != null ? new StringLiteralNode(name, value.toString()) :
56: new StringLiteralNode(value.toString());
57: break;
58: case TEMPORAL:
59: node = TemporalNodeFactory.createLiteralNode(name, value);
60: break;
61: }
62: return node;
63: }
64:
65: private static LiteralType determineLiteralType(Object value) {
66: if (value instanceof Boolean) {
67: return LiteralType.BOOLEAN;
68: } else if (value instanceof Number) {
69: return LiteralType.NUMBER;
70: } else if (value instanceof Date) {
71: return LiteralType.TEMPORAL;
72: }
73: return LiteralType.STRING;
74: }
75:
76: /**
77: * Creates collection node for the specified collection.
78: * <p>
79: * The node is without name, so it cannot be used as attribute.
80: *
81: * @param value The collection. It is used only to determine the type of the target node, no values are added to the
82: * result
83: * @return An empty collection node
84: */
85: public static CollectionNode createCollectionNode(Collection<?> value) {
86: return createCollectionNode(null, value);
87: }
88:
89: /**
90: * Creates collection node with the specified name, for the specified collection.
91: *
92: * @param name Name of the node (attribute)
93: * @param value The collection. It is used only to determine the type of the target node, no values are added to the
94: * result
95: * @return An empty collection node
96: */
97: public static CollectionNode createCollectionNode(String name, Collection<?> value) {
98: final CollectionType type = determineCollectionType(value);
99: CollectionNode n = null;
100:• switch (type) {
101: case LIST:
102:• n = name != null ? new ListNode(name) : new ListNode();
103: break;
104: case SET:
105:• n = name != null ? new SetNode(name) : new SetNode();
106: break;
107: }
108: return n;
109: }
110:
111: private static CollectionType determineCollectionType(Collection<?> collection) {
112: if (collection instanceof List) {
113: return CollectionType.LIST;
114: } else if (collection instanceof Set) {
115: return CollectionType.SET;
116: } else {
117: throw new IllegalArgumentException("Unsupported collection type " + collection.getClass());
118: }
119: }
120:
121: public static CollectionNode createCollectionNodeFromArray() {
122: return new SetNode();
123: }
124:
125: public static CollectionNode createCollectionNodeFromArray(String name) {
126: return new SetNode(name);
127: }
128:
129: public static ObjectNode createObjectNode() {
130: return new ObjectNode();
131: }
132:
133: public static ObjectNode createObjectNode(String name) {
134: return new ObjectNode(name);
135: }
136:
137: public static ObjectIdNode createObjectIdNode(Object id) {
138: return new ObjectIdNode(id.toString());
139: }
140:
141: public static ObjectIdNode createObjectIdNode(String name, Object id) {
142: return new ObjectIdNode(name, id.toString());
143: }
144: }